package com.zx.mes.hyl.common.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.net.URLDecoder;
import java.util.Date;



/**
 * 实体类相关工具类 
 * 解决问题： 1、快速对实体的常驻字段，如：crtUser、crtHost、updUser等值快速注入
 * 
 * @author hyl
 * @version 1.0
 * @date 2018-1-27
 * @since 1.8
 */
public class EntityUtils {

	private static final String REQUEST_USER_HOST = "userHost";
	private static final String REQUEST_USER_NAME = "userName";
	private static final String REQUEST_USER_ID = "userId";
	private static final String ENTITY_CRT_TIME = "crtTime";

	/**
	 * 快速将bean的crtUser、crtHost、crtTime、updUser、updHost、updTime附上相关值
	 * 
	 * @param entity 实体bean 
	 * @author
	 */
	public static <T> void setCreatAndUpdatInfo(T entity) {
		setCreateInfo(entity);
		setUpdatedInfo(entity);
	}
	
	/**
	 * 快速将bean的crtUser、crtHost、crtTime附上相关值
	 * 
	 * @param entity 实体bean
	 * @author hyl
	 */
	public static <T> void setCreateInfo(T entity){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		Request request1 = new Request(request).invoke();
		String name = request1.getName();
		String id = request1.getId();
		String hostIp = request1.getHostIp();
		// 默认属性
		String[] fields = {"crtName","crtUser","crtHost","crtTime"};
		Field field = ReflectionUtils.getAccessibleField(entity, ENTITY_CRT_TIME);
		// 默认值
		Object [] value = null;
		if(field!=null&&field.getType().equals(Date.class)){
			value = new Object []{name,id,hostIp,new Date()};
		}
		// 填充默认属性值
		setDefaultValues(entity, fields, value);
	}

	/**
	 * 快速将bean的updUser、updHost、updTime附上相关值
	 * 
	 * @param entity 实体bean
	 * @author
	 */
	public static <T> void setUpdatedInfo(T entity){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		Request request1 = new Request(request).invoke();
		String name = request1.getName();
		String id = request1.getId();
		String hostIp = request1.getHostIp();
		// 默认属性
		String[] fields = {"updName","updUser","updHost","updTime"};
		Field field = ReflectionUtils.getAccessibleField(entity, "updTime");
		Object [] value = null;
		if(field!=null&&field.getType().equals(Date.class)){
			value = new Object []{name,id,hostIp,new Date()};
		}
		// 填充默认属性值
		setDefaultValues(entity, fields, value);
	}

	/**
	 * 依据对象的属性数组和值数组对对象的属性进行赋值
	 * 
	 * @param entity 对象
	 * @param fields 属性数组
	 * @param value 值数组
	 * @author
	 */
	private static <T> void setDefaultValues(T entity, String[] fields, Object[] value) {
		for(int i=0;i<fields.length;i++){
			String field = fields[i];
			if(ReflectionUtils.hasField(entity, field)){
				ReflectionUtils.invokeSetter(entity, field, value[i]);
			}
		}
	}
	/**
	 * 根据主键属性，判断主键是否值为空
	 * 
	 * @param entity 实体类
	 * @param field 实体类中属性
	 * @return 主键为空，则返回false；主键有值，返回true
	 * @author hyl
	 * @date 2018-1-30
	 */
	public static <T> boolean isPKNotNull(T entity,String field){
		if(!ReflectionUtils.hasField(entity, field)) {
			return false;
		}
		Object value = ReflectionUtils.getFieldValue(entity, field);
		return value!=null&&!"".equals(value);
	}


	private static class Request {
		private HttpServletRequest request;
		private String hostIp;
		private String name;
		private String id;

		Request(HttpServletRequest request) {
			this.request = request;
		}

		public String getHostIp() {
			return hostIp;
		}

		public String getName() {
			return name;
		}

		public String getId() {
			return id;
		}

		public Request invoke() {
			hostIp = "";
			name = "";
			id = "";
			if(request!=null) {
                hostIp = String.valueOf(request.getHeader(REQUEST_USER_HOST));
                name = String.valueOf(request.getHeader(REQUEST_USER_NAME));
                name = URLDecoder.decode(name);
                id = String.valueOf(request.getHeader(REQUEST_USER_ID));
            }
			return this;
		}
	}
}
